package images;

import game.Configurations;
import game.TextManager;
import math.Point;
import org.newdawn.slick.opengl.Texture;

/**
 *
 * @author Luan
 */
public class MenuLoader {
    private static ImageInfo back, sel, title, item, pointer, ownerArrow, lock, currency, fi[], holder[], items[], invMenu[], arrows[], orbs[];
    private static Texture menu;   
    
    public static void load() {
        menu = ImageManager.getTexture("menu.png");
        
        float px = menu.getWidth()/menu.getImageWidth(),
              py = menu.getHeight()/menu.getImageHeight();
        back = new ImageInfo(777, 95, 6, 6, 0, px, py);
        sel = new ImageInfo(767, 95, 6, 6, 0, px, py);
        title = new ImageInfo(0, 0, 796, 72, 0, px, py);
        item = new ImageInfo(0, 72, 728, 54, 0, px, py);
        
        orbs = new ImageInfo[TextManager.t().ATTRIBUTES.length];
        for (int i = 0; i < orbs.length; i++)
            orbs[i] = new ImageInfo(50 +32*i, 200, 32, 32, 0, px, py);
        
        ownerArrow = new ImageInfo(0, 200, 32, 32, 0, px, py);
        arrows = new ImageInfo[2];
        arrows[0] = new ImageInfo(100, 200, 16, 16, 0, px, py);
        arrows[1] = new ImageInfo(100, 216, 16, 16, 0, px, py);
        
        pointer = new ImageInfo(751, 94, 8, 8, 0, px, py);
        lock = new ImageInfo(730, 85, 16, 16, 0, px, py);
        currency = new ImageInfo(755, 75, 16, 16, 0, px, py);
        
        fi = new ImageInfo[3];
        fi[0] = new ImageInfo(0, 126, 728, 8, 0, px, py); //Topo
        fi[1] = new ImageInfo(0, 134, 728, 8, 0, px, py); //Meio
        fi[2] = new ImageInfo(0, 142, 728, 8, 0, px, py); //Fim
        
        holder = new ImageInfo[4];
        holder[0] = new ImageInfo(776, 72, 15, 8, 0, px, py); //Topo
        holder[1] = new ImageInfo(776, 80, 15, 6, 0, px, py); //Meio
        holder[2] = new ImageInfo(776, 86, 15, 8, 0, px, py); //Fim
        holder[3] = new ImageInfo(728, 72, 18, 10, 0, px, py); //Pointer
        
        items = new ImageInfo[23];
        int i, j;
        for (i = 0; i < 6; i++)
            items[i] = new ImageInfo(48*i, 150, 48, 48, 0, px, py);
        for (j = 0; j < 17; j++)
            items[i + j] = new ImageInfo(48*i + 32*j, 150, 32, 32, 0, px, py);
        
        invMenu = new ImageInfo[5];
        invMenu[0] = new ImageInfo(728, 103, 32, 32, 0, px, py); //Selected Square
        invMenu[1] = new ImageInfo(760, 103, 32, 32, 0, px, py); //Regular Square

        invMenu[2] = new ImageInfo(728, 136, 5, 3, 0, px, py); //Beginning
        invMenu[3] = new ImageInfo(733, 136, 5, 3, 0, px, py); //Middle
        invMenu[4] = new ImageInfo(738, 136, 5, 3, 0, px, py); //End
    }
    
    public static void drawOrb(int t, int x, int y, int s) {
        orbs[t].draw(x, y, s, s);
    }
    
    public static int getItemHeight() {
        return item.getHeight();
    }
    
    public static int getItemWidth() {
        return item.getWidth();
    }
    
    public static int getTitleHeight() {
        return title.getHeight();
    }
    
    public static int getFiExtraHeight() {
        return fi[0].getHeight() + fi[2].getHeight();
    }
    
    public static int getInvPaddingX() {
        return invMenu[2].getWidth();
    }
    
    public static int getInvPaddingY() {
        return invMenu[2].getHeight();
    }
    
    public static void bindTexture() {
        menu.bind();
    }

    public static void drawBack() {
        back.draw(0, 0, Configurations.SCREEN_WIDTH, Configurations.SCREEN_HEIGHT);
    }
    
    public static void drawArrow(int cod, int x, int y) {
        arrows[cod].draw(x, y);
    }
    
    public static void drawOwnerArrow(int x, int y, boolean vm) {
        if (vm)
            ownerArrow.drawVMirror(x, y);
        else
            ownerArrow.draw(x, y);
    }
    
    public static void drawLock(int x, int y, int s) {
        lock.draw(x, y, s, s);
    }
    
    public static void drawBack(int x, int y, int w, int h) {
        back.draw(x, y, w, h);
    }
    
    public static void drawSel(int x, int y, int w, int h) {
        sel.draw(x, y, w, h);
    }

    public static void drawFI(int x, int y, int h) {
        drawSquare(fi, x, y, h);
    }
    
    public static void drawSelector(int x, int y) {
        pointer.draw(x - pointer.getWidth()/2, y - pointer.getHeight()/2);
    }
    
    public static void drawCurrency(int x, int y, int s) {
        currency.draw(x, y, s, s);
    }
    
    public static void drawTitle() {
        title.draw(10, 10);
    }

    public static void drawHolder(int x, int y, int h) {
        drawSquare(holder, x, y, h);
    }
    
    public static void drawInvMenu(int x, int y, int w, int qtd, int sel) {
        //Draw the bottom bar
        invMenu[2].draw(x, y - invMenu[2].getHeight());
        int i, li = x + w - invMenu[4].getWidth();
        for (i = x + invMenu[2].getWidth(); i < li; i += invMenu[3].getWidth())
            invMenu[3].draw(i, y - invMenu[3].getHeight());
        if (i != li - 1) {
            invMenu[3].draw(li - invMenu[3].getWidth(), y - invMenu[3].getHeight());
        }
        invMenu[4].draw(li, y - invMenu[4].getHeight());
        
        //Draw the squares
        for (i = 0; i < qtd; i++)
            if (i == sel)
                invMenu[0].draw(x + invMenu[2].getWidth() + (invMenu[0].getWidth() + 3)*i, y - invMenu[2].getHeight() - invMenu[0].getHeight());
            else
                invMenu[1].draw(x + invMenu[2].getWidth() + (invMenu[1].getWidth() + 3)*i, y - invMenu[2].getHeight() - invMenu[1].getHeight());
    }
    
    public static Point.Inteiro[] drawEquips(int x, int y, int s) {
        Point.Inteiro[] pos = new Point.Inteiro [7];
        
        pos[0] = new Point.Inteiro (x + 12 + s, y + 8);
        pos[1] = new Point.Inteiro (x + 8, y + 8);
        pos[2] = new Point.Inteiro (x + 16 + 2*s, y + 8);
        pos[3] = new Point.Inteiro (x + 8, y + 12 + s);
        pos[4] = new Point.Inteiro (x + 16 + 2*s, y + 12 + s);
        pos[5] = new Point.Inteiro (x + 12 + s, y + 12 + s);
        pos[6] = new Point.Inteiro (x + 24 + 3*s, y + 10 + s/2);
        
        for (int i = 0; i < pos.length; i++)
            invMenu[1].draw(pos[i].x, pos[i].y, s, s);
        
        return pos;
    }
    
    public static Point.Inteiro[] drawQuickAccess(int x, int y, int s) {
        Point.Inteiro[] pos = new Point.Inteiro [4];
        
        pos[0] = new Point.Inteiro (x + 8 + s, y + 4);
        pos[1] = new Point.Inteiro (x + 12 + 2*s, y + 6 + s/2);
        pos[2] = new Point.Inteiro (x + 8 + s, y + 8 + s);        
        pos[3] = new Point.Inteiro (x + 4, y + 6 + s/2);
       
        for (int i = 0; i < pos.length; i++)
            invMenu[1].draw(pos[i].x, pos[i].y, s, s);
        
        return pos;
    }
    
    private static void drawSquare(ImageInfo[] imgs, int x, int y, int h) {
        imgs[0].draw(x, y);
        
        int i, hn = y + imgs[0].getHeight();
        for (i = 0; i <= h - imgs[1].getHeight(); i += imgs[1].getHeight())
            imgs[1].draw(x, hn + i);
        if (i != h)
            imgs[1].draw(x, hn + h - imgs[1].getHeight());
        
        imgs[2].draw(x, hn + h);
    }
    
    public static void drawItem(int x, int y) {
        item.draw(x, y);
    }
    
    public static void drawPointer(int x, int y) {
        holder[3].draw(x, y + (item.getHeight() - holder[3].getHeight())/2);
    }
    
    public static void drawIcon(int code, int x, int y, int s) {
        if (code != -1)
            items[code].draw(x, y, s, s);
    }
}